﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ants;
using SettlersEngine;
using System.Drawing;
using System.IO;

namespace AntsBot
{
    public class Condense : Strategy
    {
        public Condense(AntLoc ant) : base(StrategyTypes.Condense, ant){ }

        public override Goal GetGoal()
        {
            if (Ant.State.MyAnts.Count <= 1)
                return new Scout(Ant).GetGoal();

            // look for friend
            var searchingspots = Ant.State.MyAnts.Where(f => Ant.State.PersistentState.Goals.Count(g => g.EndPoint.Equals(f)) <= 1);
            //searchingspots = searchingspots.Where(f => f.GetDistance(ant) < GetSearchRadius(state)).OrderBy(f => f.GetDistance(ant)).Take(2);
            var antpoint = Ant.ToPoint();
            var friendpoints = searchingspots.Select(f => new
            {
                Tile = f,
                Point = f.ToPoint(),
                Distance = Ant.State.GetDistance(Ant, f)//astar.Search(ant.ToPoint(), f.ToPoint(), new Object()) ?? Enumerable.Empty<Tile>()
            });

            var closest = friendpoints.OrderBy(f => f.Distance).FirstOrDefault();

            if (closest != null)
            {
                var goal = new Goal(closest.Tile, Utility.GetPath(Ant, closest.Tile, Ant.State), (g => g.IsUnoccupied(closest.Tile)), StrategyTypes.Condense);
                return goal;
            }

            return new SpreadOut(Ant).GetGoal();
        }
    }
}